import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_demo/page/base_page_state.dart';
import 'package:flutter_demo/page/user/login_page.dart';
import 'package:flutter_demo/page/user/order_list_page.dart';
import 'package:flutter_demo/store/app_store.dart';
import 'package:provider/provider.dart';

class UserPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return UserPageState();
  }
}

class UserPageState extends BasePageState<UserPage> {
  @override
  void initState() {
    super.initState();
    appBarController.title = '个人';
    appBarController.visible = false;
  }
  @override
  Widget buildBody(BuildContext context) {
    return CustomScrollView(
      physics: BouncingScrollPhysics(),
      slivers: <Widget>[
        SliverAppBar(
          floating: true,
          snap: true,
          pinned: true,
          stretch: true,
          expandedHeight: 250,
          flexibleSpace: FlexibleSpaceBar(
            title: Consumer<AppStore>(
              builder: (BuildContext context, AppStore app, Widget child) {
                return Text('${app.user.username}');
              },
            ),
            centerTitle: true,
            background: CachedNetworkImage(
              imageUrl: 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1531798262708&di=53d278a8427f482c5b836fa0e057f4ea&imgtype=0&src=http%3A%2F%2Fh.hiphotos.baidu.com%2Fimage%2Fpic%2Fitem%2F342ac65c103853434cc02dda9f13b07eca80883a.jpg',
              fit: BoxFit.cover,
            ),
          ),
        ),
        SliverList(
          delegate: SliverChildBuilderDelegate(_buildItem, childCount: 5),
        ),
        SliverPadding(
            padding: EdgeInsets.all(15),
            sliver: SliverToBoxAdapter(
                child: Consumer(
                    builder: (BuildContext context,  AppStore app, Widget child) {
                      return app.user.uid == null ? Container() :
                      OutlineButton(
                        child: Text(
                          '退出登录', style: TextStyle(color: Colors.redAccent),),
                        onPressed: _handleLogout,
                      );
                    }
                )
            )
        )
      ],
    );
  }

  Widget _buildItem(BuildContext context, int index) {
    return Column(
      mainAxisAlignment: MainAxisAlignment.center,
      children: <Widget>[
        InkWell(
          child: Padding(
            padding: EdgeInsets.symmetric(vertical: 10, horizontal: 15),
            child: Row(
              children: <Widget>[
                Icon(Icons.access_alarm),
                Padding(
                  padding: EdgeInsets.only(left: 10, right: 10),
                  child: Text('我的订单', style: TextStyle(fontSize: 16)),
                ),
                Expanded(
                  child: Container(),
                ),
                Image.asset('res/images/ic_right_arrow.png', width: 14, height: 14,)
              ],
            ),
          ),
          onTap: () => _handleClick(index),
        ),
        Container(
          height: 0.5,
          color: Colors.black12,
        )
      ],
    );
  }

  _handleClick(int index) {
    var user = Provider.of<AppStore>(context, listen: false).user;
    if (user.uid == null) {
      pushPage(LoginPage());
    } else {
      pushPage(OrderListPage());
    }
  }

  _handleLogout() {
    Provider.of<AppStore>(context, listen: false).user = null;
  }

  @override
  bool get wantKeepAlive => true;
}